package com.carlinfo.myjdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

import com.mysql.jdbc.Statement;

/**
  *  封装jdbc
  *  将crud中公共的方法和属性拿出来,作为公共类的属性
 * 	封装crud
 * @author CRG-LENOVO
 */
public class DbUtil
{
	private static ResourceBundle rb = ResourceBundle.getBundle("data");
	private static String driver = rb.getString("jdbc.driver");
	private static String user = rb.getString("jdbc.user");
	private static String url = rb.getString("jdbc.url");
	private static String password = rb.getString("jdbc.password");
	
	static
	{
		try
		{
			Class.forName(driver);
			System.out.println("加载驱动成功");
		} catch (ClassNotFoundException e)
		{
			System.out.println("加载驱动失败");
			e.printStackTrace();
		}
	}
	
//	public static void main(String[] args)
//	{
//		for (String key :rb.keySet() )
//		{
//			System.out.println(key+"==="+rb.getObject(key));
//		}
//	}
	
	public static Connection getConnection()
	{
		try
		{
			Connection conn = DriverManager.getConnection(url,user,password);
			return conn;
		} catch (SQLException e)
		{
			System.out.println("获取连接失败了");
			e.printStackTrace();
		}
		return null;
	}
	
	public static void closeAll(PreparedStatement st,Connection conn,ResultSet rs) 
	{
		try
		{
			if(rs!=null) 
			{
				rs.close();
				rs = null;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		try
		{
			if(st!=null)
			{
				st.close();
				st = null;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		try
		{
			if(conn!=null)
			{
				conn.close();
				conn = null;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		
	}
}
